<?php

namespace App\Libs\Company\SheetTemplateHandler\Report;

use App\Models\SubTask;

class Template113 extends AbstractTemplate
{
    const AREAS = ['台湾同胞', '香港同胞', '澳门同胞', '日本', '菲律宾', '新加坡', '泰国', '印尼', '马来西亚', '韩国', '蒙古', '印度', '其他（亚洲）', '美国', '加拿大', '其它（美洲）', '英国', '法国', '德国', '意大利', '瑞士', '瑞典', '荷兰', '俄罗斯', '西班牙', '其它（欧洲）', '澳大利亚', '新西兰', '其它（大洋洲）', '非洲', '其他'];

    public $dataStartRow = 3;

    public $rules = [
        'column' => [
            'start_row' => 3,
            'end_row' => [],
            'rule' =>
                [
                    ['column' => 2, 'vt' => 1],
                ],
        ],
        'cell' => [
        ],
        'dict' => [
            'start_row' => 3,
            'end_row' => [],
            'rule' => [
                'column' => 1,
                'vt' => 2,
                'vt_val' => self::AREAS,
            ],
        ],
    ];


    public function generateReport()
    {
        $this->renderHeader();
        $this->output['sheets']['sheet-01']['cellData']['0']['0'] = $this->createTitleCell('日喀则市' . "{$this->subTask->task->year}年{$this->subTask->task->key}月\r{$this->template->name}");
        $sn = 1;

        foreach (self::AREAS as $area) {
            $this->output['sheets']['sheet-01']['cellData'][$this->dataEndRow][0] = ['v' => $sn++, 's' => 'CENTER'];
            $this->output['sheets']['sheet-01']['cellData'][$this->dataEndRow][1] = ['v' => $area, 's' => 'CENTER'];
            $this->output['sheets']['sheet-01']['cellData'][$this->dataEndRow][2] = ['vt' => 1, 't' => 2];
            $this->dataEndRow++;
        }

        $this->dataEndRow--;
        $this->renderFooter();
        return $this->output;
    }

    public function getData()
    {
        if (empty($this->subTask) || $this->subTask->status != SubTask::STATUS_COMPLETED) {
            throw new \Exception('工单状态不正确');
        }
        $data = [];
        $this->dataEndRow = $this->dataStartRow;
        while (1) {
            if (!isset($this->subTask->data['sheets']['sheet-01']['cellData'][$this->dataEndRow])) {
                break;
            }
            $row = $this->subTask->data['sheets']['sheet-01']['cellData'][$this->dataEndRow++];
            if (!is_numeric($row[0]['v'])) {
                break;
            }
            $data[$row[1]['v']] = $row;
        }
        $this->dataEndRow--;
        return $data;
    }
}
